package com.spring.myspace.web.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.google.common.collect.Lists;
import com.spera.genutil.TokenUtil;
import com.spring.myspace.web.dao.MySpaceMapper;
import com.spring.myspace.web.model.Album;
import com.spring.myspace.web.model.AlbumPhoto;
import com.spring.myspace.web.model.FileNeedDelete;
import com.spring.myspace.web.model.Journal;
import com.spring.myspace.web.model.SetVideo;
import com.spring.myspace.web.service.MySpaceCommon;
import com.spring.myspace.web.service.MySpaceService;
import com.spring.security.core.util.ApplicationUtils;
import com.spring.security.web.dao.CommonMapper;
import com.spring.security.web.dao.JedisCache;
import com.spring.security.web.model.BaseResult;
import com.spring.tools.MyTools;
import com.sso.api.web.dao.SsoMapper;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
@Service
public class MySpaceServiceImpl  implements MySpaceService{
	@Resource
	private MySpaceMapper mySpaceMapper;
	
	@Resource
	private MySpaceCommon mySpaceCommon;
	
    @Resource
    private JedisCache jedisCache;
    
    @Resource
    private CommonMapper commonMapper;
    
	@Resource
	private SsoMapper ssoMapper;

	// 生成日志
	@Override
	public Map<String, Object> createJournal(String mapAppType,String osType,String token, String c_family_id,
			String c_family_member_id, String title, String content,String ispublic,String latitude,
			String longitude,String location)
			throws Exception {

		Map<String, Object> m = new HashMap<String, Object>();
		
		if(ispublic==null||ispublic.equals("")){
			ispublic="1";
		}
		
		Map<String, Object> t = TokenUtil.checkToken(token, jedisCache);
		if (t != null) {
			return t;
		}
		String c_user_id = jedisCache.hget(token, TokenUtil.USER_ID);
		String family_id = jedisCache.hget(token, TokenUtil.FAMILY_ID);
		if(MyTools.checkIsNotNull(c_family_id)!=null){
			c_family_id=family_id;
		}
		
//		String c_user_name = jedisCache.hget(token, TokenUtil.USER_NAME);
		String logintType = jedisCache.hget(token, TokenUtil.LOGIN_TYPE);
		String member_name = jedisCache.hget(token, TokenUtil.MEMBER_NAME);
		
//		String c_user_id = jedisCache.lindex(token, 0);

		HashMap<String,Object> hmc = MyTools.checkIsNotNull( 
				title, content,osType);
		if (ApplicationUtils.isNotNull(hmc)) {
			return hmc;
		}
		
		String uuid = ApplicationUtils.randomUUID();

		HashMap<String,Object> hm = new HashMap<String,Object>();
		hm.put("uuid", uuid);
		hm.put("c_family_id", c_family_id);
		hm.put("mapAppType", mapAppType);
		hm.put("osType", osType);
//		hm.put("c_family_member_id", c_user_id);
		hm.put("c_family_member_id", c_family_member_id);
		hm.put("c_family_member_name", member_name);
		hm.put("title", title);
		hm.put("content", content);
		hm.put("ispublic", ispublic);
		hm.put("latitude", latitude);
		hm.put("longitude", longitude);
		hm.put("location", location);
		
		
		if(TokenUtil.CUS_LOGIN.equalsIgnoreCase(logintType)){
			hm.put("publishUserTypeCode", "1");
			hm.put("publishUserId", c_user_id);
		}else if(TokenUtil.OA_LOGIN.equalsIgnoreCase(logintType)){
			hm.put("publishUserTypeCode", "2");
			hm.put("publishUserId", c_user_id);
		}

		int effect = mySpaceMapper.createJournal(hm);

		if (effect == 1) {

			List list = new ArrayList();
			HashMap<String, Object> hm2 = new HashMap<String, Object>();
			hm2.put("my_space_journal_id", uuid);
			list.add(hm2);

			m.put(ApplicationUtils.DATASET, list);
			m.put(ApplicationUtils.STATUS, "1");
			m.put(ApplicationUtils.MSG, "插入成功");
		} else {
			m.put(ApplicationUtils.STATUS, "0");
			m.put(ApplicationUtils.MSG, "插入失败");
		}

		return m;
	}
	
	
	// 添加图片
	@Override
	public Map<String, Object> addJournalPhoto(String token,
			String my_space_journal_id, String c_family_member_id,String file) throws Exception {
		Map<String, Object> m = new HashMap();

		Map<String, Object> t = TokenUtil.checkToken(token, jedisCache);
		if (t != null) {
			return t;
		}

//		String c_user_id = jedisCache.lindex(token, 0);
		
		HashMap hmc = MyTools.checkIsNotNull(my_space_journal_id, file);
		if (ApplicationUtils.isNotNull(hmc)) {
			return hmc;
		}
		
		
/*		//检验是否有每天相册
		String my_space_album_id=mySpaceCommon.checkJournal(c_family_member_id);
		
		if(!ApplicationUtils.isNotNull(my_space_album_id)){
			m.put(ApplicationUtils.STATUS, "0");
			m.put(ApplicationUtils.MSG, "生成每天日志失败");
			return m;
		}*/
		
		
		
		JSONArray json = JSONArray.fromObject(file);

		// 循环插入数据
		for (int i = 0; i < json.size(); i++) {

			JSONObject jsonObject = json.getJSONObject(i);

			System.out.println(jsonObject.get("file_id"));

			String file_id = (String) jsonObject.get("file_id");

			HashMap hm = new HashMap();
			hm.put("my_space_journal_id", my_space_journal_id);
			hm.put("file_id", file_id);
			//hm.put("my_space_album_id", my_space_album_id);

			// 检验id
			int count = mySpaceMapper.addJournalPhotoCheckFileId(file_id);

			if (count != 1) {
				m.put(ApplicationUtils.STATUS, "0");
				m.put(ApplicationUtils.MSG, "请输入有效的file_id");
				return m;
			}
			// 检验id
			int count2 = mySpaceMapper
					.addJournalPhotoCheckJournalId(my_space_journal_id);

			if (count2 != 1) {
				m.put(ApplicationUtils.STATUS, "0");
				m.put(ApplicationUtils.MSG, "请输入有效的my_space_journal_id");
				return m;
			}
			
			//检验重复
			int count3=mySpaceMapper.addJournalPhotoCheckDuplicate(hm);
			if (count3 == 1) {
				m.put(ApplicationUtils.STATUS, "0");
				m.put(ApplicationUtils.MSG, "请不要重复插入数据");
				return m;
			}

			// 插入日志图片
			int effect = mySpaceMapper.addJournalPhoto(hm);
			
			if (effect == 1) {
				m.put(ApplicationUtils.STATUS, "1");
				m.put(ApplicationUtils.MSG, "插入成功");
			} else {
				//插入失败抛出异常
				throw new Exception();
			}
			
/*			//插入每天相册
			int effect2=mySpaceMapper.addJournalAlbumPhoto(hm);
			if (effect2 == 1) {

				m.put(ApplicationUtils.STATUS, "1");
				m.put(ApplicationUtils.MSG, "插入成功");
			} else {
				//插入失败抛出异常
				throw new Exception();
			}*/
			

		}

		return m;
	}
		/**
		 * 添加日志视频
		 */
		@Override
		public Map<String, Object> addJournalVideo(String token,String c_family_member_id, String my_space_journal_id,
				String file_id) throws Exception {
			Map<String, Object> m = new HashMap();

			Map<String, Object> t = TokenUtil.checkToken(token, jedisCache);
			if (t != null) {
				return t;
			}

//			String c_user_id = jedisCache.lindex(token, 0);

			HashMap hmc = MyTools.checkIsNotNull(my_space_journal_id, file_id);
			if (ApplicationUtils.isNotNull(hmc)) {
				return hmc;
			}

			HashMap hm = new HashMap();
			hm.put("my_space_journal_id", my_space_journal_id);
			hm.put("file_id", file_id);
			hm.put("c_family_member_id", c_family_member_id);
			
			
//			int count=mySpaceMapper.checkVideoID(file_id);
//			
//			if(count!=1){
//				m.put(ApplicationUtils.STATUS, "0");
//				m.put(ApplicationUtils.MSG, "请输入有效的file_id");
//				return m;
//			}
			
			int count2=mySpaceMapper.addJournalPhotoCheckJournalId(my_space_journal_id);
			
			
			if(count2!=1){
				m.put(ApplicationUtils.STATUS, "0");
				m.put(ApplicationUtils.MSG, "请输入有效的my_space_journal_id");
				return m;
			}
			
			
	/*		int count3=mySpaceMapper.addJournalVideoCheckDuplicate(hm);
			
			if(count3>=1){
				m.put(ApplicationUtils.STATUS, "0");
				m.put(ApplicationUtils.MSG, "不能重复插入");
				return m;
			}
			*/
			
			//插入日志视频
			int effect = mySpaceMapper.addJournalVideo(hm);

			if (effect != 1) {
				throw new Exception();
			} 
			
			//插入视频
			int effect2=mySpaceMapper.addSetVideo(hm);
			
			if (effect2 != 1) {
				throw new Exception();
			} 
			

			m.put(ApplicationUtils.STATUS, "1");
			m.put(ApplicationUtils.MSG, "插入成功");
			
			return m;
		}
		
		
		
		/**
		 * 添加相册视频封面
		 */
		@Override
		public Map<String, Object> addJournalVideoCover(String token, String my_space_video_id, String file_id,
				String c_family_member_id) throws Exception {
			Map<String, Object> m = new HashMap();

			Map<String, Object> t = TokenUtil.checkToken(token, jedisCache);
			if (t != null) {
				return t;
			}

//			String c_user_id = jedisCache.lindex(token, 0);

			HashMap hmc = MyTools.checkIsNotNull(my_space_video_id, file_id,c_family_member_id);
			if (ApplicationUtils.isNotNull(hmc)) {
				return hmc;
			}

			HashMap hm = new HashMap();
			hm.put("my_space_video_id", my_space_video_id);
			hm.put("file_id", file_id);
			hm.put("c_family_member_id", c_family_member_id);
			
			//验证文件id
			int count=mySpaceMapper.addJournalPhotoCheckFileId(file_id);
			
			if(count!=1){
				m.put(ApplicationUtils.STATUS, "0");
				m.put(ApplicationUtils.MSG, "请输入有效的file_id");
				return m;
			}
			//验证视频id
			int count2=mySpaceMapper.addJournalVideoCoverCheckVideoId(hm);
			
			
			if(count2<1){
				m.put(ApplicationUtils.STATUS, "0");
				m.put(ApplicationUtils.MSG, "请输入有效的my_space_video_id");
				return m;
			}
			
			
			int effect=mySpaceMapper.addJournalVideoCoverUpdateFile(hm);
			
			if(effect>=1){
				m.put(ApplicationUtils.STATUS, "1");
				m.put(ApplicationUtils.MSG, "修改成功");
			}else{
				m.put(ApplicationUtils.STATUS, "0");
				m.put(ApplicationUtils.MSG, "修改失败");
			}
			
			
			/*int count3=mySpaceMapper.addJournalVideoCoverGetCount(hm);
			
			if(count3>=1){
				//更新表
				int effect=mySpaceMapper.addJournalVideoCoverUpdateFile(hm);
				
					if(effect>=1){
						m.put(ApplicationUtils.STATUS, "1");
						m.put(ApplicationUtils.MSG, "修改成功");
					}else{
						m.put(ApplicationUtils.STATUS, "0");
						m.put(ApplicationUtils.MSG, "修改失败");
					}
				
				
			}else{
				//插入表
				int effect=mySpaceMapper.addJournalVideoCover(hm);
				if(effect>=1){
					m.put(ApplicationUtils.STATUS, "1");
					m.put(ApplicationUtils.MSG, "插入成功");
				}else{
					m.put(ApplicationUtils.STATUS, "0");
					m.put(ApplicationUtils.MSG, "插入失败");
				}
			}
			*/
			
			return m;
		}
		
		
		
		
		
		
		
		/**
		 * 查询日志
		 */
	@Override
	public Map<String, Object> getJournalList(String token,
			String c_family_member_id, String page_size, String page_index,
			String skip, String rows) throws Exception {
		Map<String, Object> m = new HashMap<String, Object>();

		Map<String, Object> t = TokenUtil.checkToken(token, jedisCache);
		if (t != null) {
			return t;
		}


		String c_user_id = jedisCache.hget(token, TokenUtil.USER_ID);
	
		HashMap<String, Object> hm = MyTools.pageIndexToSkip(page_size, page_index, skip, rows);
		
		String[] ids=ssoMapper.getIDsByID(c_user_id);
		
		hm.put("userList", ids);

		List<Journal> list = Lists.newArrayList();
		if(ids.length>0){
			list = mySpaceMapper.getJournalList(hm);
		}
		
		m.put(ApplicationUtils.DATASET, list);
		m.put(ApplicationUtils.STATUS, "1");
		m.put(ApplicationUtils.MSG, "查询成功");

		return m;
	}
	
	/**
	 * 删除日志
	 */
@Override
public Map<String, Object> deleteJournal(String token,
		String my_space_journal_id) throws Exception {
	Map<String, Object> m = new HashMap<String, Object>();

	Map<String, Object> t = TokenUtil.checkToken(token, jedisCache);
	if (t != null) {
		return t;
	}

	String cUserID = jedisCache.hget(token, TokenUtil.USER_ID);
	String familyID = jedisCache.hget(token, TokenUtil.FAMILY_ID);

	HashMap<String, Object> hmc = MyTools.checkIsNotNull(my_space_journal_id);
	if (ApplicationUtils.isNotNull(hmc)) {
		return hmc;
	}
	
	/*暂时不验证
	 * String c_family_member_id2=mySpaceMapper.deleteJournalCheckId(hm);
	
	if(c_family_member_id2==null){
		m.put(ApplicationUtils.STATUS, "0");
		m.put(ApplicationUtils.MSG, "请输入有效的id");
		return m;
	}
	
	if(!c_family_member_id2.equals(c_family_member_id)){
		m.put(ApplicationUtils.STATUS, "0");
		m.put(ApplicationUtils.MSG, "只能删除自己的日志");
		return m;
	}
	*/
	Map<String,Object> hm=new HashMap<String,Object>();
	hm.put("my_space_journal_id", my_space_journal_id);
	hm.put("cUserID", cUserID);
	hm.put("familyID", familyID);
	
	//删除各个关联表
	//List<FileNeedDelete> list = mySpaceMapper.deleteJournalPhotoGetFile(my_space_journal_id);
	int effect = mySpaceMapper.deleteJournalPhoto(hm);
	//mySpaceCommon.checkIsNeedDelete(list);
	
	//删除视频
	//List<FileNeedDelete> list2=mySpaceMapper.deleteJournalVideoCoverGetFile(my_space_journal_id);
	//List<FileNeedDelete> list3=mySpaceMapper.deleteJournalVideoGetFile(my_space_journal_id);
	int effect3 = mySpaceMapper.deleteJournalVideo(hm);
	//mySpaceCommon.checkIsNeedDelete(list2);
	//System.out.println("------->");
	//mySpaceCommon.checkIsNeedDelete(list3);

	
	int effect4 = mySpaceMapper.deleteJournal(hm);

	
	if(effect4<1){
		throw new Exception();
	}

	m.put(ApplicationUtils.STATUS, "1");
	m.put(ApplicationUtils.MSG, "删除成功");

	return m;
}


@Override
public BaseResult deleteTeacherJournal(String token,
		String my_space_journal_id) throws Exception {
	BaseResult br=null;

	br = TokenUtil.checkTokenBR(token, jedisCache);
	if (br != null) {
		return br;
	}
	
	br = MyTools.checkIsNotNullBR(my_space_journal_id);
	if (ApplicationUtils.isNotNull(br)) {
		return br;
	}
	
	String cUserID = jedisCache.hget(token, TokenUtil.USER_ID);
	
	Map<String,Object> hm=new HashMap<String,Object>();
	hm.put("my_space_journal_id", my_space_journal_id);
	hm.put("cUserID", cUserID);

	mySpaceMapper.deleteTeacherJournalVideo(hm);
	mySpaceMapper.deleteTeacherJournalPhoto(hm);
	int effect=mySpaceMapper.deleteTeacherJournal(hm);
	if(effect<1){
		throw new Exception();
	}
	return new BaseResult("1","删除成功");
	
}

/**
 * 删除图片
 */
@Override
public Map<String, Object> deleteJournalPhoto(String token,
	String photo) throws Exception {
Map<String, Object> m = new HashMap();

Map<String, Object> t = TokenUtil.checkToken(token, jedisCache);
if (t != null) {
	return t;
}

//String c_family_member_id = jedisCache.lindex(token, 0);

HashMap hmc = MyTools.checkIsNotNull(photo);
if (ApplicationUtils.isNotNull(hmc)) {
	return hmc;
}


JSONArray json = JSONArray.fromObject(photo);

// 循环删除数据
for (int i = 0; i < json.size(); i++) {

	JSONObject jsonObject = json.getJSONObject(i);

	System.out.println(jsonObject.get("photo_id"));

	String photo_id = (String) jsonObject.get("photo_id");


/*暂不验证
 * 验证权限
String c_family_member_id2=mySpaceMapper.deleteSingleeJournalPhotoCheckId(photo_id);

if(c_family_member_id2==null){
	m.put(ApplicationUtils.STATUS, "0");
	m.put(ApplicationUtils.MSG, "请输入有效的id");
	return m;
}

if(!c_family_member_id2.equals(c_family_member_id)){
	m.put(ApplicationUtils.STATUS, "0");
	m.put(ApplicationUtils.MSG, "只能删除自己的图片");
	return m;
}
*/


//删除文件 数据库 文件表
String file_id=mySpaceMapper.deleteSingleeJournalPhotoGetFileId(photo_id);
int effect=mySpaceMapper.deleteSingleeJournalPhoto(photo_id);

if(effect==1){
	int status= mySpaceCommon.checkIsNeedDelete(file_id);
	m.put(ApplicationUtils.STATUS, "1");
	m.put(ApplicationUtils.MSG, "删除成功");
}else{
	m.put(ApplicationUtils.STATUS, "1");
	m.put(ApplicationUtils.MSG, "删除失败");
	return m;
}



}

return m;
}






/**
 * 删除日志视频
 */
@Override
public Map<String, Object> deleteJournalVideo(String token,
	String video_id) throws Exception {
Map<String, Object> m = new HashMap();

Map<String, Object> t = TokenUtil.checkToken(token, jedisCache);
if (t != null) {

	return t;

}

//String c_family_member_id = jedisCache.lindex(token, 0);

HashMap hmc = MyTools.checkIsNotNull(video_id);
if (ApplicationUtils.isNotNull(hmc)) {
	return hmc;
}




/*暂不验证
 * 验证权限
String c_family_member_id2=mySpaceMapper.deleteSingleeJournalVideoCheckId(video_id);

if(c_family_member_id2==null){
	m.put(ApplicationUtils.STATUS, "0");
	m.put(ApplicationUtils.MSG, "请输入有效的id");
	return m;
}

if(!c_family_member_id2.equals(c_family_member_id)){
	m.put(ApplicationUtils.STATUS, "0");
	m.put(ApplicationUtils.MSG, "只能删除自己的图片");
	return m;
}
*/


//删除封面文件
String vidoe_cover_file_id=mySpaceMapper.deleteSingleeJournalVideoCoverGetFileId(video_id);
/*int effect=mySpaceMapper.deleteSingleeJournalVideoCover(video_id);

if(effect==1){
	int status= mySpaceCommon.checkIsNeedDelete(vidoe_cover_file_id);
}*/

//删除视频文件
String vidoe_file_id=mySpaceMapper.deleteSingleeJournalVideoGetFileId(video_id);
int effect2=mySpaceMapper.deleteSingleeJournalVideo(video_id);


if(effect2==1){
	int status= mySpaceCommon.checkIsNeedDelete(vidoe_file_id);
	int status2= mySpaceCommon.checkIsNeedDelete(vidoe_cover_file_id);
	m.put(ApplicationUtils.STATUS, "1");
	m.put(ApplicationUtils.MSG, "删除成功");
}else{
	m.put(ApplicationUtils.STATUS, "1");
	m.put(ApplicationUtils.MSG, "删除失败");
	throw new Exception();
	
}

/*System.out.println("成功");
if(1==1){
throw new Exception();
}
*/

return m;
}



	/**
	 * 修改日志
	 */
	@Override
	public Map<String, Object> updateJournal(String token,
			String my_space_journal_id, String title, String content)
			throws Exception {
		Map<String, Object> m = new HashMap();

		Map<String, Object> t = TokenUtil.checkToken(token, jedisCache);
		if (t != null) {
			return t;
		}

		String c_family_member_id = jedisCache.hget(token, TokenUtil.USER_ID);

		HashMap hmc = MyTools.checkIsNotNull(my_space_journal_id);
		if (ApplicationUtils.isNotNull(hmc)) {
			return hmc;
		}

		HashMap hm = new HashMap();
		hm.put("my_space_journal_id", my_space_journal_id);
		hm.put("c_family_member_id", c_family_member_id);
		hm.put("title", title);
		hm.put("content", content);

		/* 暂不验证验证权限
		String c_family_member_id2 = mySpaceMapper.deleteJournalCheckId(hm);

		if (c_family_member_id2 == null) {
			m.put(ApplicationUtils.STATUS, "0");
			m.put(ApplicationUtils.MSG, "请输入有效的id");
			return m;
		}

		if (!c_family_member_id2.equals(c_family_member_id)) {
			m.put(ApplicationUtils.STATUS, "0");
			m.put(ApplicationUtils.MSG, "只能修改自己的日志");
			return m;
		}
		*/
		

		// 删除修改日志
		int effect = mySpaceMapper.updateJournal(hm);

		if (effect == 1) {
			m.put(ApplicationUtils.STATUS, "1");
			m.put(ApplicationUtils.MSG, "修改成功");
		} else {
			m.put(ApplicationUtils.STATUS, "1");
			m.put(ApplicationUtils.MSG, "修改失败/或者没有变化");
		}

		return m;
	}
	
	
	
	
	
	@Override
	public Map<String, Object> createAlbum(String token, String name, String desc, 
			String c_family_id, String c_family_member_id, String isfamily,String ispublic) throws Exception {
		Map<String, Object> m = new HashMap();

		Map<String, Object> t = TokenUtil.checkToken(token, jedisCache);
		if (t != null) {
			return t;
		}
		
//		System.out.println("xxx");
		
		String c_family_member_id2 = jedisCache.hget(token, TokenUtil.USER_ID);
		
		if(c_family_member_id==null){
			c_family_member_id=c_family_member_id2;
		}
		
		HashMap hmc = MyTools.checkIsNotNull(name,c_family_id,c_family_member_id,isfamily,ispublic);
		if (ApplicationUtils.isNotNull(hmc)) {
			return hmc;
		}

		HashMap hm = new HashMap();
		hm.put("name", name);
		hm.put("desc", desc);
		hm.put("c_family_id", c_family_id);
		hm.put("c_family_member_id", c_family_member_id);
		hm.put("isfamily", isfamily);
		hm.put("ispublic", ispublic);
	

		// 创建相册
		int effect = mySpaceMapper.createAlbum(hm);

		if (effect == 1) {
			m.put(ApplicationUtils.STATUS, "1");
			m.put(ApplicationUtils.MSG, "添加成功");
		} else {
			m.put(ApplicationUtils.STATUS, "1");
			m.put(ApplicationUtils.MSG, "添加失败");
		}

		return m;
	}
	
	//相册图片添加（图片迁移、每天普通相册相互迁移）
	@Override
	public Map<String, Object> addAlbumPhoto(String token, String my_space_album_id, 
			String c_family_member_id, String photo) throws Exception {
		Map<String, Object> m = new HashMap();

		Map<String, Object> t = TokenUtil.checkToken(token, jedisCache);
		if (t != null) {
			return t;
		}

		String c_family_member_id2 = jedisCache.hget(token, TokenUtil.USER_ID);

		HashMap hmc = MyTools.checkIsNotNull(my_space_album_id,photo);
		if (ApplicationUtils.isNotNull(hmc)) {
			return hmc;
		}
		
		JSONArray json = JSONArray.fromObject(photo);

		// 循环添加数据
		for (int i = 0; i < json.size(); i++) {
						
			
			JSONObject jsonObject = json.getJSONObject(i);

			System.out.println(jsonObject.get("photo_file_id"));

			String photo_file_id = (String) jsonObject.get("photo_file_id");
		
		
		HashMap hm = new HashMap();
		hm.put("my_space_album_id", my_space_album_id);
		hm.put("c_family_member_id", c_family_member_id);
		hm.put("file_id", photo_file_id);

	/**验证重复	int count=mySpaceMapper.addAlbumPhotoCheckId(hm);
		
		if(count==0){
			
		}*/
	if(i==0){
	int cou=mySpaceMapper.addAlbumPhotoCheckCount(hm);
	if(cou==0){
		this.setAlbumCover(token, my_space_album_id, photo_file_id);
	}
		}
			
		
		
int effect=mySpaceMapper.addAlbumPhoto(hm);
			
	}
		m.put(ApplicationUtils.STATUS, "1");
		m.put(ApplicationUtils.MSG, "添加成功");
	

	return m;
	}
	
	
	
	//修改相册图片名
	@Override
	public Map<String, Object> updateAlbumPhoto(String token, String photo_id, 
			String name) throws Exception {
		Map<String, Object> m = new HashMap();

		Map<String, Object> t = TokenUtil.checkToken(token, jedisCache);
		if (t != null) {
			return t;
		}

		String c_family_member_id2 = jedisCache.hget(token, TokenUtil.USER_ID);

		HashMap hmc = MyTools.checkIsNotNull(photo_id,name);
		if (ApplicationUtils.isNotNull(hmc)) {
			return hmc;
		}
			
		HashMap hm = new HashMap();
		hm.put("photo_id", photo_id);
		hm.put("name", name);

		int effect=mySpaceMapper.updateAlbumPhoto(hm);
		
		if(effect==1){
			m.put(ApplicationUtils.STATUS, "1");
			m.put(ApplicationUtils.MSG, "修改成功");	
		}else{
			m.put(ApplicationUtils.STATUS, "0");
			m.put(ApplicationUtils.MSG, "修改失败或没有变化");	
			
		}

	return m;
	}
	
	
	
	
	
	
	//设置封面
		@Override
		public Map<String, Object> setAlbumCover(String token, String my_space_album_id,
				String file_id) throws Exception {
			Map<String, Object> m = new HashMap();

			Map<String, Object> t = TokenUtil.checkToken(token, jedisCache);
			if (t != null) {
				return t;
			}

			String c_family_member_id2 = jedisCache.hget(token, TokenUtil.USER_ID);

			HashMap hmc = MyTools.checkIsNotNull(my_space_album_id,file_id);
			if (ApplicationUtils.isNotNull(hmc)) {
				return hmc;
			}
				
			HashMap hm = new HashMap();
			hm.put("file_id", file_id);
			hm.put("my_space_album_id", my_space_album_id);
			
		//	int effect=mySpaceMapper.removeAlbumCover(hm);		
			
			int effect2=mySpaceMapper.setAlbumCover(hm);
			
			if(effect2==1){
				m.put(ApplicationUtils.STATUS, "1");
				m.put(ApplicationUtils.MSG, "修改成功");	
			}else{
				throw new Exception();	
			}

		return m;
		}

	// 相册查询
	@Override
	public Map<String, Object> getAlbumList(String token, String isfamily,
			String c_family_member_id, String c_family_id,String my_space_album_id) throws Exception {
		Map<String, Object> m = new HashMap();

		Map<String, Object> t = TokenUtil.checkToken(token, jedisCache);
		if (t != null) {
			return t;
		}

		String c_family_member_id2 = jedisCache.hget(token, TokenUtil.USER_ID);
		
//		if(c_family_member_id==null){
//			c_family_member_id=c_family_member_id2;
//		}

		HashMap hmc = MyTools.checkIsNotNull(isfamily);
		if (ApplicationUtils.isNotNull(hmc)) {
			return hmc;
		}

		// 验证必输参数
		if (isfamily.equals("1")) {

			HashMap hmc2 = MyTools.checkIsNotNull(c_family_id);
			if (ApplicationUtils.isNotNull(hmc)) {
				return hmc2;
			}

		} else if (isfamily.equals("0")) {

			HashMap hmc3 = MyTools.checkIsNotNull(c_family_member_id);
			if (ApplicationUtils.isNotNull(hmc)) {
				return hmc3;
			}
		} else {
			m.put(ApplicationUtils.STATUS, "0");
			m.put(ApplicationUtils.MSG, "isfamily必须为1或0");
		}

		HashMap hm = new HashMap();
		hm.put("isfamily", isfamily);
		hm.put("c_family_member_id", c_family_member_id);
		hm.put("c_family_id", c_family_id);
		hm.put("my_space_album_id", my_space_album_id);
		

		List<Album> list = mySpaceMapper.getAlbumList(hm);
		
		m.put(ApplicationUtils.DATASET, list);
		m.put(ApplicationUtils.STATUS, "1");
		m.put(ApplicationUtils.MSG, "查询成功");

		return m;
	}
	
	
	
	
	// 相册查询
		@Override
		public Map<String, Object> getAlbumPhotoList(String token, String my_space_album_id, 
				String page_size, String page_index, String skip, String rows) throws Exception {
			Map<String, Object> m = new HashMap();

			Map<String, Object> t = TokenUtil.checkToken(token, jedisCache);
			if (t != null) {
				return t;
			}

			String c_family_member_id2 = jedisCache.hget(token, TokenUtil.USER_ID);

			HashMap hmc = MyTools.checkIsNotNull(my_space_album_id);
			if (ApplicationUtils.isNotNull(hmc)) {
				return hmc;
			}

			HashMap hm =MyTools.pageIndexToSkip(page_size, page_index, skip, rows);

			hm.put("my_space_album_id", my_space_album_id);

			List<AlbumPhoto> list = mySpaceMapper.getAlbumPhotoList(hm);
			
			m.put(ApplicationUtils.DATASET, list);
			m.put(ApplicationUtils.STATUS, "1");
			m.put(ApplicationUtils.MSG, "查询成功");

			return m;
		}
	
		
		
	/**
	 * 删除相册图片
	 */
	@Override
	public Map<String, Object> deleteAlbumPhoto(String token, String photo)
			throws Exception {
		Map<String, Object> m = new HashMap();

		Map<String, Object> t = TokenUtil.checkToken(token, jedisCache);
		if (t != null) {
			return t;
		}

		String c_family_member_id = jedisCache.hget(token, TokenUtil.USER_ID);

		HashMap hmc = MyTools.checkIsNotNull(photo);
		if (ApplicationUtils.isNotNull(hmc)) {
			return hmc;
		}

		JSONArray json = JSONArray.fromObject(photo);

		// 循环删除数据
		for (int i = 0; i < json.size(); i++) {

			JSONObject jsonObject = json.getJSONObject(i);

			System.out.println(jsonObject.get("photo_id"));

			String photo_id = (String) jsonObject.get("photo_id");

			
			
			// 删除文件 数据库 文件表
			String file_id = mySpaceMapper.deleteAlbumPhotoGetFileId(photo_id);
			int effect = mySpaceMapper.deleteAlbumPhoto(photo_id);

			if (effect == 1) {
				int status = mySpaceCommon.checkIsNeedDelete(file_id);
				m.put(ApplicationUtils.STATUS, "1");
				m.put(ApplicationUtils.MSG, "删除成功");
			} else {
				m.put(ApplicationUtils.STATUS, "1");
				m.put(ApplicationUtils.MSG, "删除失败");
				return m;
			}

		}

		return m;
	}

	
	/**
	 * 删除相册
	 */
	@Override
	public Map<String, Object> deleteAlbum(String token,
			String my_space_album_id) throws Exception {
		Map<String, Object> m = new HashMap();

		Map<String, Object> t = TokenUtil.checkToken(token, jedisCache);
		if (t != null) {
			return t;
		}

		String c_family_member_id = jedisCache.hget(token, TokenUtil.USER_ID);

		HashMap hmc = MyTools.checkIsNotNull(my_space_album_id);
		if (ApplicationUtils.isNotNull(hmc)) {
			return hmc;
		}

		HashMap hm = new HashMap();

	

	
		
		//删除各个关联表
		List<FileNeedDelete> list = mySpaceMapper.deleteAlbumGetFileId(my_space_album_id);
		int effect = mySpaceMapper.deleteAllAlbumPhoto(my_space_album_id);
		
		
		
		int effect2=mySpaceMapper.deleteAlbum(my_space_album_id);
		
		System.out.println(effect2);
		
		if(effect2!=1){
			throw new Exception();
		}
		
		System.out.println(list.size());
		
		//确认主表删除后删除文件
		if(list!=null&&list.size()>0){
			if((FileNeedDelete)list.get(0)!=null){
				mySpaceCommon.checkIsNeedDelete(list);
			}
			
		}


		m.put(ApplicationUtils.STATUS, "1");
		m.put(ApplicationUtils.MSG, "删除成功");

		return m;
	}

	
	/**
	 * 修改相册
	 */
	@Override
	public Map<String, Object> updateAlbum(String token, String name, String desc,
			String my_space_album_id) throws Exception {
		Map<String, Object> m = new HashMap();

		Map<String, Object> t = TokenUtil.checkToken(token, jedisCache);
		if (t != null) {
			return t;
		}

		String c_family_member_id = jedisCache.hget(token, TokenUtil.USER_ID);

		HashMap hmc = MyTools.checkIsNotNull(my_space_album_id);
		if (ApplicationUtils.isNotNull(hmc)) {
			return hmc;
		}

		HashMap hm = new HashMap();
		hm.put("desc", desc);
		hm.put("name", name);
		hm.put("my_space_album_id", my_space_album_id);

	
		int effect = mySpaceMapper.updateAlbum(hm);
		
		

		
		if(effect==1){
			m.put(ApplicationUtils.STATUS, "1");
			m.put(ApplicationUtils.MSG, "修改成功");
		}else{
			m.put(ApplicationUtils.STATUS, "0");
			m.put(ApplicationUtils.MSG, "修改失败或没有变化");
		}
		

		return m;
	}
	
	
	
	/**
	 * 添加日志视频
	 */
	@Override
	public Map<String, Object> addVideo(String token, String file_id, String c_family_member_id, 
			String c_family_id, String isfamily, String title, String content) throws Exception {
		Map<String, Object> m = new HashMap();

		Map<String, Object> t = TokenUtil.checkToken(token, jedisCache);
		if (t != null) {
			return t;
		}

		String c_user_id = jedisCache.hget(token, TokenUtil.USER_ID);
		if(!ApplicationUtils.isNotNull(c_family_member_id)){
			c_family_member_id = c_user_id;
		}
		HashMap hmc = MyTools.checkIsNotNull(file_id, c_family_member_id,c_family_id,isfamily);
		if (ApplicationUtils.isNotNull(hmc)) {
			return hmc;
		}
		
		String uuid=ApplicationUtils.randomUUID();
		
		HashMap hm = new HashMap();
		hm.put("uuid", uuid);
		hm.put("file_id", file_id);
		hm.put("c_family_member_id", c_family_member_id);
		hm.put("c_family_id", c_family_id);
		hm.put("isfamily", isfamily);
		hm.put("title", title);
		hm.put("content", content);
//		System.out.println(hm.toString());
//		int count=mySpaceMapper.addJournalPhotoCheckFileId(file_id);
//		
//		if(count!=1){
//			m.put(ApplicationUtils.STATUS, "0");
//			m.put(ApplicationUtils.MSG, "请输入有效的file_id");
//			return m;
//		}
		int count2=mySpaceMapper.addVideo(hm);
		
		if(count2!=1){
			m.put(ApplicationUtils.STATUS, "0");
			m.put(ApplicationUtils.MSG, "插入失败");
			return m;
		}
		
		HashMap hmr=new HashMap();
		hmr.put("video_id", uuid);
		
		List list=new ArrayList();
		list.add(hmr);
		
		m.put(ApplicationUtils.DATASET,list);
		m.put(ApplicationUtils.STATUS, "1");
		m.put(ApplicationUtils.MSG, "插入成功");
		return m;
	}
	
	

	/**
	 * 添加日志视频封面
	 */
	@Override
	public Map<String, Object> addVideoCover(String token, String video_id, String file_id, 
			String c_family_member_id) throws Exception {
		Map<String, Object> m = new HashMap();

		Map<String, Object> t = TokenUtil.checkToken(token, jedisCache);
		if (t != null) {
			return t;
		}

		String c_user_id = jedisCache.hget(token, TokenUtil.USER_ID);

		HashMap hmc = MyTools.checkIsNotNull(video_id, file_id,c_family_member_id);
		if (ApplicationUtils.isNotNull(hmc)) {
			return hmc;
		}
		

		
		HashMap hm = new HashMap();
		hm.put("video_id", video_id);
		hm.put("file_id", file_id);
		hm.put("c_family_member_id", c_family_member_id);
	
		System.out.println(hm.toString());
		
	//	int effect=mySpaceMapper.deleteVideoCover(hm);
		int count=mySpaceMapper.addVideoCover(hm);
		
		if(count!=1){
		//	throw new Exception();
		}
	
		
		m.put(ApplicationUtils.STATUS, "1");
		m.put(ApplicationUtils.MSG, "插入成功");
		
		return m;
	}
	
	
	/**
	 * 修改视频内容
	 */
	@Override
	public Map<String, Object> updateVideo(String token, String title, String content, 
			String video_id) throws Exception {
		Map<String, Object> m = new HashMap();

		Map<String, Object> t = TokenUtil.checkToken(token, jedisCache);
		if (t != null) {
			return t;
		}

		String c_user_id = jedisCache.hget(token, TokenUtil.USER_ID);

		HashMap hmc = MyTools.checkIsNotNull(video_id);
		if (ApplicationUtils.isNotNull(hmc)) {
			return hmc;
		}
		

		
		HashMap hm = new HashMap();
		hm.put("title", title);
		hm.put("content", content);
		hm.put("video_id", video_id);
	
		System.out.println(hm.toString());
		
		int effect=mySpaceMapper.updateVideo(hm);
		
		
	
		if(effect==1){
		m.put(ApplicationUtils.STATUS, "1");
		m.put(ApplicationUtils.MSG, "修改成功");
		}else{
		 m.put(ApplicationUtils.STATUS, "0");
	     m.put(ApplicationUtils.MSG, "修改失败或没有变化");
		}
		
		
		return m;
	}
	
	
	/**
	 * 得到视频列表
	 * @param token
	 * @param isfamily
	 * @param c_family_member_id
	 * @param c_family_id
	 * @param page_size
	 * @param page_index
	 * @param skip
	 * @param rows
	 * @return
	 * @throws Exception
	 */
	@Override
	public Map<String, Object> getVideoList(String token, String isfamily, 
			String c_family_member_id, String c_family_id, String page_size, 
			String page_index, String skip, String rows) throws Exception {
		Map<String, Object> m = new HashMap();

		Map<String, Object> t = TokenUtil.checkToken(token, jedisCache);
		if (t != null) {
			return t;
		}

		String c_user_id = jedisCache.hget(token,TokenUtil.USER_ID);
		if(!ApplicationUtils.isNotNull(c_family_member_id)){
			c_family_member_id = c_user_id;
		}
		HashMap hmc = MyTools.checkIsNotNull(isfamily,c_family_member_id);
		if (ApplicationUtils.isNotNull(hmc)) {
			return hmc;
		}
		
		HashMap hm =MyTools.pageIndexToSkip(page_size, page_index, skip, rows);
		hm.put("isfamily", isfamily);
		hm.put("c_family_member_id", c_family_member_id);
		hm.put("c_family_id", c_family_id);
	
//		System.out.println(hm.toString());
		
		List<SetVideo> list =mySpaceMapper.getVideoList(hm);
		
		m.put(ApplicationUtils.DATASET, list);
		m.put(ApplicationUtils.STATUS, "1");
		m.put(ApplicationUtils.MSG, "查询成功");

		return m;
	}
	
	/**
	 *删除视频
	 */
	@Override
	public Map<String, Object> deleteVideo(String token,
			String video_id) throws Exception {
		Map<String, Object> m = new HashMap();
		Map<String, Object> t = TokenUtil.checkToken(token, jedisCache);
		if (t != null) {
			return t;
		}
		String c_family_member_id = jedisCache.hget(token, TokenUtil.USER_ID);
		HashMap hmc = MyTools.checkIsNotNull(video_id);
		if (ApplicationUtils.isNotNull(hmc)) {
			return hmc;
		}
		HashMap hm = new HashMap();
		hm.put("video_id", video_id);
		
		//删除封面得到文件id
//       	String cover_file_id=mySpaceMapper.deleteVideoCoverGetFileId(hm);
       	
//       	//删除封面
//       	UpdateMap u = new UpdateMap("my_space_set_video_cover");
//   	 	u.addWhere("my_space_set_video_id", video_id);
//       	int i = commonMapper.delete(u);
       	

		
       	//删除视频得到文件id
		List<Map<String,Object>> file_id_map=mySpaceMapper.deleteVideoGetFileId(hm);
		
		//删除视频
				int effect=mySpaceMapper.deleteVideo(hm);
			
				if(effect!=1){
					throw new Exception();
				}
		
		
		
		
		if(file_id_map!=null&&file_id_map.size()>0){
			Map<String,Object> vm = file_id_map.get(0);
			//确认主表删除后删除文件
			mySpaceCommon.checkIsNeedDelete(ApplicationUtils.Object2String(vm.get("base_videos_file_id")));
			mySpaceCommon.checkIsNeedDelete(ApplicationUtils.Object2String(vm.get("cover_file_id")));
		}

//		if(1==1){
//			throw new Exception();}

		m.put(ApplicationUtils.STATUS, "1");
		m.put(ApplicationUtils.MSG, "删除成功");

		return m;
	}
	
}
	
	

	
	
		

